<?php
/**
 * Created by PhpStorm.
 * User: aeball
 * Date: 2017/3/19
 * Time: 11:22
 */

namespace app\api\controller;
use app\common\controller\HomeBase;
use think\View;
use think\Request;
use app\common\model\Photo as PhotoModel;
use app\common\model\Slide as SlideModel;
use app\common\model\Nav as NavModel;
use think\Controller;
use think\Paginator;

class Photo extends HomeBase
{
    private $_photo_model;
    private $_slide_model;
    private $_nav_model;

    public function _initialize(){
        $this->_photo_model = new PhotoModel();
        $this->_slide_model = new SlideModel();
        $this->_nav_model = new NavModel();
    }

    /*
     * 首页json数据
     * */
    public function index($limit = 5){
        /*
         * banner图
         * */
        $banner = $this->_slide_model->showList();
        $order = ['create_time' => 'DESC'];
        $nav = $this->_nav_model->getLevelList();
        $data_array['nav'] = $nav;
        foreach($nav as $k => $v){
            $where = "status = 0 and cid = ".$v['id'];
            $content = $this->_photo_model->showPages($limit,$where,$order);
            $data_array['nav'][$k]['images'] = $content;
        }
        // echo "<pre>";
        // print_r($data_array['nav']);
        // echo "</pre>";
        $data_array['banner'] = $banner;
        
//return $this->thinkJson($this->_photo_model->showHomeList());
        // return json_encode($data_array);
       return $this->thinkJson($data_array);
    }

    public function nav($limit = 10,$cid){
        $banner = $this->_slide_model->showList();
        // $cid = $this->request->param('cid');
        // $cid = 1;
        $where = "cid = {$cid} and status = 0";
        $order = ['create_time' => 'DESC'];
        $banner = $this->_slide_model->showList();
        $content = $this->_photo_model->showPages($limit,$where,$order);
        $totalPage = $this->_photo_model->getTotalPage($limit,$where);
        $nav = $this->_nav_model->getLevelList();
        $data_array['banner'] = $banner;
        $data_array['content'] = $content;
        $data_array['nav'] = $nav;
        $data_array['totalpage'] = $totalPage;
//return $this->thinkJson($this->_photo_model->showHomeList());
        // return json_encode($data_array);
       return $this->thinkJson($data_array);
    }

    /*
     * 根据知乎日报修改json格式
     * */
    public function zhihu($page = 1){
        $photo_list = $this->_photo_model->zhihu_list($page);
        // 取出里面的图片解析之后放进  http://www.qknykj.cn
//        var_dump($photo_list);
        print_r($photo_list);



    }

    /*
     * 详情页面json数据
     * */
    public function detail($id){
        $photo_data = $this->_photo_model->showOneArticle($id);
        $photo_array = $photo_data['0'];
        $photos_arr = unserialize($photo_array['photo']);
        $photo_array['photo'] = $photos_arr;

        return $this->thinkJson($photo_array);
    }

}